package com.hooyach.spring_ai_demo.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.net.http.HttpClient;

/**
 * ClassName: AiController
 * Package: com.hooyach.spring_ai_demo.controller
 * Description:
 *
 * @author 胡雅琪
 * Create 2024/9/13 11:01
 * Version 1.0
 */
@RestController
@RequestMapping("/ai")
public class AiController {
    @Resource
    ChatClient chatClient;
    @Resource
    ChatModel chatModel;
    @Resource
    OpenAiImageModel openaiImageModel;


    public AiController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    /**
     * 串行输出
     * @param msg
     * @return
     */
    @GetMapping("/chat")
    public String chat(@RequestParam(value = "msg", defaultValue = "你好呀") String msg) {
        System.out.println("开始调用第三方接口");
        System.out.println("msg=" + msg);
        try {
            String content = chatClient.prompt()
                    .user(msg)
                    //一次性全部返回 同步
                    .call()
                    .content();
            System.out.println("API 响应: " + content);  // 打印 API 的原始响应
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            return "Error occurred: " + e.getMessage();
        }


    }

    /**
     * 流式输出
     * @param msg
     * @return
     */
    @GetMapping(value = "/stream",produces = "text/html;charset=UTF-8")
    public Flux<String> stream(@RequestParam(value = "msg", defaultValue = "你好呀") String msg) {
        System.out.println("开始调用第三方接口,用流的方式");
        System.out.println("msg=" + msg);
        return  chatClient.prompt()
                .user(msg)
                //流式返回 异步
                .stream()
                .content();
    }

    /**
     * 通用模型
     * @return
     */
    @GetMapping(value = "/model/call",produces = "text/html;charset=UTF-8")
    public String modelCall(@RequestParam(value = "msg", defaultValue = "你好呀") String msg) {
        System.out.println("开始调用第三方接口");
        //使用默认的模型
        ChatResponse response = chatModel.call(new Prompt(
                msg,
                OpenAiChatOptions.builder()
                        .withModel("gpt-4")
                        .withTemperature(0.8F)
                        .build()
        ));
        return response.getResult().getOutput().getContent();
    }

   @GetMapping(value = "/img")
   public String img(@RequestParam(value = "msg", defaultValue = "画一只小猫咪") String msg) {
       System.out.println("开始文字生成图片");
       ImageResponse response = openaiImageModel.call(
               new ImagePrompt(msg,
                       OpenAiImageOptions.builder()
                               .withQuality("hd")
                               .withN(1)
                               .withHeight(1024)
                               .withWidth(1024).build())

       );
       return response.getResult().getOutput().getUrl();
   }







    @GetMapping("/test")
    public String test() {
        return "hello ai";
    }
}

